<?php

class PostsController extends Controller {
    var $name = 'Posts';
    var $uses = array('Posts','Post2category', 'Attachments');
    var $components = array('page','diagram','json', 'attachment', 'posts');

    function index() {
        $this->action = 'posts';
        $this->posts();
    }

    function posts($category = null, $page = 1, $edit_status = null) {
        if ($category == 'edit_post_done') {
            $edit_status = 'edit_post_done';
            $category = 0;
        } else if ($page == 'edit_post_done') {
            $edit_status = 'edit_post_done';
            $page = 1;
        }

        switch ($edit_status) {
            case 'edit_post_done':
                $this->header_notice = __('Your post has been saved.');
                break;
            case 'deleted_done':
                $this->header_notice = __('Your posts has been successfully deleted.');
                break;
            case 'deleted_failed':
                $this->header_notice = __('Deleted failed, You haven\'t permission.');
                break;
        }

        $category = intval($category);
        $posts = current_user_can_write_posts($category, $page);
        $this->set('posts', $posts);
        $select_list = get_diagram_as_select(array('selected'=>$category, 'none'=>__('All')));
        $this->set('select_list', $select_list);
        $this->set('category_id', $category);
        $this->set('page', $page);
        $this->set('this_url', $this->__posts_url());
    }

    function ajax_posts($category = null, $page = 1, $ids = null) {
        $this->layout = null;
        $i = 0;

        if (!empty($ids)) {
            $ids = trim($ids, '|');
            $ids = explode('|', $ids);
            if (!empty($ids)) {
                foreach($ids as $id) {
                    if (current_user_can('Post', 'delete', $id)) {
                        $this->Posts->del($id);
                        $this->Post2category->del_all("post_id='$id'");
                        delete_attachments($id);
                        $i++;
                    }
                }
            }
        }
        $json_data['deleted_count'] = $i;
        $posts = current_user_can_write_posts($category, $page);
        $json = new Services_JSON();
        $output = $json->encode($json_data);
        echo $output;
    }

    function ajax_change_star($post_id) {
        $this->layout = null;
        $this->Posts->id = $post_id;
        $post = $this->Posts->read();
        if (empty($post->star_status)) {
            $star_status = 1;
            $message = 'on';
        } else {
            $star_status = 0;
            $message = 'off';
        }
        $this->Posts->save_field('star_status', $star_status);
        echo $message;
    }

    function __posts_url() {
        $posts_url = str_replace('edit_post_done', '', $this->url());
        $posts_url = str_replace('deleted_done', '', $this->url());
        $posts_url = rtrim($posts_url, '/');
        if ($posts_url == '/posts') {
            $posts_url = '/posts/posts';
        }
        return $posts_url;
    }
}

?>
